package pogvue.analysis;

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import pogvue.datamodel.*;
import pogvue.io.*;
import pogvue.gui.*;
import pogvue.gui.hub.*;
import pogvue.gui.event.*;
import pogvue.analysis.*;

public class SequenceFetchThread extends Thread  {
  Controller controller;
  AlignViewport viewport;
  
  ActionListener l;
  
  public SequenceFetchThread(Controller controller, AlignViewport viewport) {
	this.controller = controller;
	this.viewport   = viewport;
    }
  
  public void setActionListener(ActionListener l) {
    this.l = l;
  }
  
  public void run() {

    getSequence();
    
    if (l != null) {
      ActionEvent e = new ActionEvent(this,0,"Done");
      
      l.actionPerformed(e);
    }
  }

  public void getSequence() {
    if (viewport.getAlignment().getSequenceRegion() != null) {
      SequenceRegion r = viewport.getAlignment().getSequenceRegion();
      
      int start = r.getStart() + viewport.getStartRes() - 20000;
      int end   = r.getStart() + viewport.getEndRes() + 20000;
      
      if (start < r.getStart()) {
	start = r.getStart();
      }
      if (end > r.getEnd()) {
	end = r.getEnd();
      }
      
      int tmpstart = start - r.getStart();
      int tmpend   = end - r.getStart();
      
      String regstr = "query="  + r.getChr() + 
	"&start=" + start +
	"&end="   + end +
	"&z=2";
      
      System.out.println("Getting region " + regstr);
      
      try {
	    

	System.out.println("Getting region " + tmpstart + " " + tmpend + " " + (tmpend-tmpstart+1));

	//setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

	Hashtable       nameHash = viewport.getAlignment().getNameHash();
	GappedFastaFile ff       = GenomeInfoFactory.getRegion(regstr);
	//FastaFile ff       = GenomeInfoFactory.getHumanRegion(regstr);
	    
	ff.parse();

	int j = 0;
	    
	SequenceI[] newseqs = ff.getSeqsAsArray();
	Vector      seqs    = viewport.getAlignment().getSequences();
	    
	int         count    = 0;
	int         lastseq  = 0;
	Vector      novelseq = new Vector();
	    
	while (j < newseqs.length) {
	  if (nameHash.containsKey(newseqs[j].getName())) {
	    Sequence s = (Sequence)nameHash.get(newseqs[j].getName());
	    String seq = s.getSequence();
	    
	    if (seq.length() > tmpend+1) {
	      String newseq = seq.substring(0,tmpstart) + newseqs[j].getSequence() + seq.substring(tmpend+1);
	      s.setSequence(newseq);
	    }
	  } else {
	    novelseq.addElement(newseqs[j]);
	  }
	  j++;
	}
	    
	j = 0;
	    
	while (j < seqs.size()) {
	  Sequence s = (Sequence)seqs.elementAt(j);
	  
	  if (s.getSequence().length() > 0) {
	    lastseq = j;
	  }
	  j++;
	}
	    
	lastseq++;

	for (int i = 0; i < novelseq.size(); i++) {
	  Sequence     novseq = (Sequence)novelseq.elementAt(i);
	  StringBuffer seqstr = new StringBuffer();
	      
	  for (int jj = 0; jj < viewport.getAlignment().getWidth(); jj++) {
	    seqstr.append("X");
	  }
	      
	  String   str    = seqstr.toString();
	  String   newseq = str.substring(0,tmpstart) + novseq.getSequence() + str.substring(tmpend);
	      
	  Sequence tmpseq = new Sequence(novseq.getName(),newseq,1,newseq.length());
	      
	  seqs.insertElementAt(tmpseq,lastseq);
	      
	  lastseq++;
	}
	    
	viewport.setVisibleSequence(true);
	    
	for (int i = 0; i < viewport.getAlignment().getHeight(); i++) {
	  if (viewport.getAlignment().getSequenceAt(i).getSequence().length() > 0) {
	    viewport.showSequence(viewport.getAlignment().getSequenceAt(i));
	  }
	}
		    
      } catch (IOException err) {
	err.printStackTrace();
      }
    }
  }
}